//
//  CommentCell.swift
//  ShareUmbrella
//
//  Created by duanmingyang on 2017/9/18.
//  Copyright © 2017年 尧尚信息科技. All rights reserved.
//

import UIKit
import SDWebImage

class CommentCell: UITableViewCell {

    let kScreenHeight = UIScreen.main.bounds.size.height
    let kScreenWidth = UIScreen.main.bounds.size.width
    
    var avtorImg:UIImageView?
    var titleLabel:UILabel?
    var dateLabel:UILabel?
    var commentLabel:UILabel?
    var startView: TQStarRatingView!
    
    required init?(coder aDecoder:NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style:UITableViewCellStyle, reuseIdentifier:String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.white
        self.setUpUI();
    }
    
    func setUpUI() {
        
        //头像
        avtorImg = UIImageView.init(frame: CGRect(x: 28, y: 15, width: 50, height: 50))
        avtorImg?.layer.masksToBounds = true
        avtorImg?.layer.cornerRadius = (avtorImg?.frame.size.height)!/2.0
        avtorImg?.image = UIImage.init(named: "头像")
        self.addSubview(avtorImg!)
        
        //位置信息
        titleLabel = UILabel.init()
        titleLabel?.backgroundColor = UIColor.clear;
        titleLabel?.frame = CGRect(x:106, y:15, width:200, height:20)
        titleLabel?.text = "小明"
        titleLabel?.textColor = RGB(r: 120, 120, 120)
        titleLabel?.font = UIFont.systemFont(ofSize: 18)
        addSubview(self.titleLabel!)
        
        //位置信息
        dateLabel = UILabel.init()
        dateLabel?.backgroundColor = UIColor.clear;
        dateLabel?.frame = CGRect(x:kScreenWidth-220, y:15, width:200, height:20)
        dateLabel?.text = "2017-09-19"
        dateLabel?.textColor = RGB(r: 120, 120, 120)
        dateLabel?.textAlignment = NSTextAlignment.right
        dateLabel?.font = UIFont.systemFont(ofSize: 14)
        addSubview(self.dateLabel!)
        
        //星星
        //let v = createStartView(n: 4)
        //v.frame = CGRect(x: 106, y: 30, width: 200, height: 40)
        //addSubview(v)
        
        startView = TQStarRatingView(frame: CGRect(x: 106, y: 38, width: 75, height: 14)).ys.customize { (view) in
            self.addSubview(view)
        }
        startView.setScore(0.7, withAnimation: false)
        
        //位置信息
        commentLabel = UILabel.init()
        commentLabel?.backgroundColor = UIColor.clear;
        commentLabel?.frame = CGRect(x:106, y:55, width:kScreenWidth-106-20, height:20)
        commentLabel?.text = ""
        commentLabel?.textColor = RGB(r: 120, 120, 120)
        commentLabel?.font = UIFont.systemFont(ofSize: 14)
        commentLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
        commentLabel?.numberOfLines = 0
        //commentLabel?.sizeToFit()
        addSubview(self.commentLabel!)
    }
    
    //RGB
    func RGB(r:CGFloat,_ g:CGFloat,_ b:CGFloat) -> UIColor{
        return UIColor(red: (r)/255.0, green: (g)/255.0, blue: (b)/255.0, alpha: 1.0)
    }
    
    //创建星星
    func createStartView(n:NSInteger,_ h:CGFloat = 40) -> UIView{
        let v = UIView.init()
        v.backgroundColor = UIColor.clear
        
        for index in 0...n {
            let iv = UIImageView.init(frame: CGRect(x: index*15, y: Int((h-14)/2), width: 15, height: 14))
            iv.image = UIImage.init(named: "星形_Mark")
            v.addSubview(iv)
        }
        
        return v
    }
    
    func setData(name:String,picture:String,star:String,time:String,comment:String){
        self.avtorImg?.sd_setImage(with: URL(string: picture),placeholderImage:  UIImage(named: "头像"), options: SDWebImageOptions.highPriority, completed: nil)
        self.titleLabel?.text = name
        self.dateLabel?.text = time
        self.commentLabel?.text = comment
        if(comment == ""){
            self.commentLabel?.text = "无评论"
        }
        self.commentLabel?.sizeToFit()
        self.startView.setScore(Float(star.double()/5.0), withAnimation: false)
    }

}
